package umich.max.geolocation.geonames;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;

@SuppressWarnings("unchecked")
public class GeonamesIndexer {

	private final String admin2IndexDir = "/home/max/proj/geoloc/indeces/admin2";
	private final String admin2File = "/home/max/resources/admin2Codes.txt";
	private IndexWriter admin2IndexWriter;

	private final String featsIndexDir = "/home/max/proj/geoloc/indeces/feats";
	private final String featsFile = "/home/max/resources/featureCodes_en.txt";
	private IndexWriter featsIndexWriter;

	public static void main(String[] args) {
		GeonamesIndexer g = new GeonamesIndexer();
		// g.indexAdmin2Codes();
		g.indexFeats();
	}

	public GeonamesIndexer() {
		admin2IndexWriter = new Index(admin2IndexDir).createIndexWriter();
		featsIndexWriter = new Index(featsIndexDir).createIndexWriter();

	}

	public void indexAdmin2Codes() {
		try {
			File f = new File(admin2File);
			List<String> lines = FileUtils.readLines(f, "UTF-8");

			for (String line : lines) {
				// sample line: US.CT.013 Tolland County Tolland County 4844275
				if (line.startsWith("US")) {
					String[] sections = line.split("\t");
					if (sections.length == 4) {

						// We need to find the admin2 codes, and other info for
						// our lucene index
						String[] sectionsOfAdminCode = sections[0].split("\\.");
						String country = sectionsOfAdminCode[0];
						String state = sectionsOfAdminCode[1];
						String admin2code = sectionsOfAdminCode[2];
						String name = sections[1];
						String asciiname = sections[2];
						Integer geonameID = Integer.parseInt(sections[3]);

						// add them to our lucene document
						Document document = new Document();
						document.add(new StringField("country", country,
								Field.Store.YES));
						document.add(new StringField("state", state,
								Field.Store.YES));
						document.add(new StringField("admin2", admin2code,
								Field.Store.YES));
						document.add(new StringField("name", name,
								Field.Store.YES));
						document.add(new StringField("asciiname", asciiname,
								Field.Store.YES));
						document.add(new IntField("geonameID", geonameID,
								Field.Store.YES));

						// write it to the index
						admin2IndexWriter.addDocument(document);

					}
				}
			}
			admin2IndexWriter.commit();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void indexFeats() {
		try {
			File f = new File(featsFile);
			List<String> lines = FileUtils.readLines(f, "UTF-8");

			for (String line : lines) {
				// sample line: S.HUTS huts small primitive houses
				// split into sectoins
				String[] sections = line.split("\t");
				String code = sections[0];
				String name = sections[1];
				Document document = new Document();

				if (sections.length > 2){
				String definition = sections[2];
					document.add(new StringField("definition", definition,
							Field.Store.YES));
				}
				document.add(new StringField("code", code, Field.Store.YES));
				document.add(new StringField("name", name, Field.Store.YES));
			

				// write it to the index
				featsIndexWriter.addDocument(document);

			}
			featsIndexWriter.commit();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
